package com.dc.trans.steps.ftpoutput;

import java.util.List;
import java.util.Map;

import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.Counter;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.w3c.dom.Node;

import com.dc.adaptor.transport.ftp.FtpMeta;

public class FTPOutputMeta extends BaseStepMeta implements StepMetaInterface,FtpMeta{
	
	private String serverName;
	private String port;
	private String userName;
	private String password;
	private String ftpDirectory;
	private String wildcard;
	private boolean remove;
	private String targetDirectory;
	private String  controlEncoding;
	private int     timeout;
	private boolean activeConnection;
	private boolean binaryMode;
	private String target;
	private String messageConvertor;

	static private String LEGACY_CONTROL_ENCODING = "US-ASCII";
    
    /**
     * Default encoding when making a new ftp job entry instance.
     */
    static private String DEFAULT_CONTROL_ENCODING = "ISO-8859-1";  

	@Override
	public void check(List<CheckResultInterface> remarks, TransMeta transMeta,
			StepMeta stepMeta, RowMetaInterface prev, String[] input,
			String[] output, RowMetaInterface info) {
		// TODO Auto-generated method stub
		
	}

	public StepInterface getStep(StepMeta stepMeta,
			StepDataInterface stepDataInterface, int copyNr,
			TransMeta transMeta, Trans trans) {
		// TODO Auto-generated method stub
		return new FTPOutput(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}

	public StepDataInterface getStepData() {
		// TODO Auto-generated method stub
		return new FTPOutputData();
	}

	public void loadXML(Node stepnode, List<DatabaseMeta> databases,
			Map<String, Counter> counters) throws KettleXMLException {
		try
		{
			port          = XMLHandler.getTagValue(stepnode, "port");
			serverName          = XMLHandler.getTagValue(stepnode, "servername");
			userName            = XMLHandler.getTagValue(stepnode, "username");
		    password = Encr.decryptPasswordOptionallyEncrypted(XMLHandler.getTagValue(stepnode, "password")); 
			ftpDirectory        = XMLHandler.getTagValue(stepnode, "ftpdirectory");
			targetDirectory     = XMLHandler.getTagValue(stepnode, "targetdirectory");
			wildcard            = XMLHandler.getTagValue(stepnode, "wildcard");
			target            = XMLHandler.getTagValue(stepnode, "target");
			messageConvertor            = XMLHandler.getTagValue(stepnode, "messageConvertor");
			binaryMode          = "Y".equalsIgnoreCase( XMLHandler.getTagValue(stepnode, "binary") );
			timeout             = Const.toInt(XMLHandler.getTagValue(stepnode, "timeout"), 10000);
			remove              = "Y".equalsIgnoreCase( XMLHandler.getTagValue(stepnode, "remove") );
            activeConnection    = "Y".equalsIgnoreCase( XMLHandler.getTagValue(stepnode, "active") );
            controlEncoding     = XMLHandler.getTagValue(stepnode, "control_encoding");
            if ( controlEncoding == null )
            {
            	// if we couldn't retrieve an encoding, assume it's an old instance and
            	// put in the the encoding used before v 2.4.0
            	controlEncoding = LEGACY_CONTROL_ENCODING;
            }     
		}
		catch(Exception e)
		{
			throw new KettleXMLException("Unable to load step info from XML", e);
		}
	}

	public void readRep(Repository rep, ObjectId id_step,
			List<DatabaseMeta> databases, Map<String, Counter> counters)
			throws KettleException {
		port          = rep.getStepAttributeString(id_step, "port");
		serverName          = rep.getStepAttributeString(id_step, "servername");
		userName            = rep.getStepAttributeString(id_step, "username");
		password = Encr.decryptPasswordOptionallyEncrypted( rep.getStepAttributeString(id_step, "password") );
		ftpDirectory        = rep.getStepAttributeString(id_step, "ftpdirectory");
		targetDirectory     = rep.getStepAttributeString(id_step, "targetdirectory");
		wildcard            = rep.getStepAttributeString(id_step, "wildcard");
		target            = rep.getStepAttributeString(id_step, "target");
		messageConvertor            = rep.getStepAttributeString(id_step, "messageConvertor");
		binaryMode          = rep.getStepAttributeBoolean(id_step, "binary");
		timeout             = (int)rep.getStepAttributeInteger(id_step, "timeout");
        remove              = rep.getStepAttributeBoolean(id_step, "remove");
        activeConnection    = rep.getStepAttributeBoolean(id_step, "active");
        controlEncoding     = rep.getStepAttributeString(id_step, "control_encoding");
        if ( controlEncoding == null )
        {
        	// if we couldn't retrieve an encoding, assume it's an old instance and
        	// put in the the encoding used before v 2.4.0
        	controlEncoding = LEGACY_CONTROL_ENCODING;
        }
	}

	public void saveRep(Repository rep, ObjectId id_transformation,
			ObjectId id_step) throws KettleException {
		
		rep.saveStepAttribute(id_transformation, id_step, "port",      port);
		rep.saveStepAttribute(id_transformation, id_step, "servername",      serverName);
		rep.saveStepAttribute(id_transformation, id_step, "username",        userName);
		rep.saveStepAttribute(id_transformation, id_step, "password", Encr.encryptPasswordIfNotUsingVariables(password));
		rep.saveStepAttribute(id_transformation, id_step, "ftpdirectory",    ftpDirectory);
		rep.saveStepAttribute(id_transformation, id_step, "targetdirectory", targetDirectory);
		rep.saveStepAttribute(id_transformation, id_step, "wildcard",        wildcard);
		rep.saveStepAttribute(id_transformation, id_step, "target",        target);
		rep.saveStepAttribute(id_transformation, id_step, "messageConvertor",        messageConvertor);
		rep.saveStepAttribute(id_transformation, id_step, "binary",          binaryMode);
		rep.saveStepAttribute(id_transformation, id_step, "timeout",         timeout);
        rep.saveStepAttribute(id_transformation, id_step, "remove",          remove);
        rep.saveStepAttribute(id_transformation, id_step, "active",          activeConnection);
        rep.saveStepAttribute(id_transformation, id_step, "control_encoding",controlEncoding);
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public void setDefault() {
		
	}
	public String getXML()
	{
		StringBuilder retval=new StringBuilder();
		
		retval.append("      ").append(XMLHandler.addTagValue("port",   port));
		retval.append("      ").append(XMLHandler.addTagValue("servername",   serverName));
		retval.append("      ").append(XMLHandler.addTagValue("username",     userName));
	    retval.append("      ").append(XMLHandler.addTagValue("password", Encr.encryptPasswordIfNotUsingVariables(password)));
		retval.append("      ").append(XMLHandler.addTagValue("ftpdirectory", ftpDirectory));
		retval.append("      ").append(XMLHandler.addTagValue("targetdirectory", targetDirectory));
		retval.append("      ").append(XMLHandler.addTagValue("wildcard",     wildcard));
		retval.append("      ").append(XMLHandler.addTagValue("target",     target));
		retval.append("      ").append(XMLHandler.addTagValue("messageConvertor",     messageConvertor));
		retval.append("      ").append(XMLHandler.addTagValue("binary",       binaryMode));
		retval.append("      ").append(XMLHandler.addTagValue("timeout",      timeout));
		retval.append("      ").append(XMLHandler.addTagValue("remove",       remove));
        retval.append("      ").append(XMLHandler.addTagValue("active",       activeConnection));
        retval.append("      ").append(XMLHandler.addTagValue("control_encoding",  controlEncoding));

		return retval.toString();
	}


	
	
	@Override
	public void getFields(RowMetaInterface inputRowMeta, String name,
			RowMetaInterface[] info, StepMeta nextStep, VariableSpace space)
			throws KettleStepException {
		ValueMetaInterface vPack = new ValueMeta("CONTENT",ValueMetaInterface.TYPE_STRING);
		inputRowMeta.addValueMeta(vPack);
	}
	

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFtpDirectory() {
		return ftpDirectory;
	}

	public void setFtpDirectory(String ftpDirectory) {
		this.ftpDirectory = ftpDirectory;
	}

	public String getWildcard() {
		return wildcard;
	}

	public void setWildcard(String wildcard) {
		this.wildcard = wildcard;
	}

	public boolean isRemove() {
		return remove;
	}

	public void setRemove(boolean remove) {
		this.remove = remove;
	}

	public String getTargetDirectory() {
		return targetDirectory;
	}

	public void setTargetDirectory(String targetDirectory) {
		this.targetDirectory = targetDirectory;
	}

	public String getControlEncoding() {
		return controlEncoding;
	}

	public void setControlEncoding(String controlEncoding) {
		this.controlEncoding = controlEncoding;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public boolean isActiveConnection() {
		return activeConnection;
	}

	public void setActiveConnection(boolean activeConnection) {
		this.activeConnection = activeConnection;
	}

	public boolean isBinaryMode() {
		return binaryMode;
	}

	public void setBinaryMode(boolean binaryMode) {
		this.binaryMode = binaryMode;
	}

	public String getMessageConvertor() {
		return messageConvertor;
	}

	public void setMessageConvertor(String messageConvertor) {
		this.messageConvertor = messageConvertor;
	}

	@Override
	public String getFileEncoding() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getBackupDirectory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isBackup() {
		// TODO Auto-generated method stub
		return false;
	}
	
}
